package com.cl.web.action;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cl.bean.Emp;
import com.cl.service.UserService;
import com.cl.web.base.BaseAction;

@Controller
@Scope("prototype")
public class UserAction extends BaseAction<Emp> {

	@Autowired
	private UserService userService;

	private String code;

	public void setCode(String code) {
		this.code = code;
	}

	public String login() {
		String validatecode = (String) ServletActionContext.getRequest().getSession().getAttribute("key");
		if (code.equals(validatecode)) {
			Emp user = userService.login(model);
			if (user != null) {
				ServletActionContext.getRequest().getSession().setAttribute("user", user);
				return HOME;
			} else {
				this.addActionError("用户名或者密码输入错误！");
				return LOGIN;
			}
		}else{
			this.addActionError("验证码错误！");
			return LOGIN;
		}
	}
	
	public String logout(){
		ServletActionContext.getRequest().getSession().invalidate();
		return LOGIN;
	}
}
